﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GenerateTagLinks
{
    public partial class Form1 : Form
    {
        private List<string> _keyWords = new List<string>();
        private List<string> _urls = new List<string>();
        private Dictionary<string, string> _backLinks = new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataProvider.InputPath = Application.StartupPath + "\\seo.xml";
            if (!DataProvider.LoadDBInfo(ref _keyWords, ref _urls))
            {
                MessageBox.Show("File xml khong hop le!");
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtKeyword.Text == "" && txtUrl.Text == "")
            {
                MessageBox.Show("Vui long nhap du lieu");
                return;
            }
            var errTxt = DataProvider.AddNewNode(txtKeyword.Text.Trim(), txtUrl.Text.Trim());
            if (errTxt == "")
            {
                DataProvider.LoadDBInfo(ref _keyWords, ref _urls);
                MessageBox.Show("Da them");
            }
            else
            {
                MessageBox.Show(errTxt);
            }
        }

        public List<String> GetRandomItemsFromList(List<String> list, int returnCount)
        {
            List<String> returnList = new List<String>();
            Dictionary<int, int> randoms = new Dictionary<int, int>();

            while (randoms.Count != returnCount)
            {
                int randomInt = new Random().Next(list.Count);

                try
                {
                    randoms.Add(randomInt, randomInt);
                }
                catch (ArgumentException aex)
                {
                    Console.Write(aex.Message);
                } 

                if (randoms.Count == returnCount)
                {
                    foreach (int key in randoms.Keys)
                        returnList.Add(list[randoms[key]]);

                    break; 
                }
            }

            return returnList;
        }

        public void GenerateBacklinks(ref List<string> lstKeyword, ref List<string> lstUrl)
        {
            int numElem = _keyWords.Count > _urls.Count ? _urls.Count : _keyWords.Count;
            if (numElem == _keyWords.Count)
            {
                lstKeyword = _keyWords;
                lstUrl = GetRandomItemsFromList(_urls, numElem);
            }
            else
            {
                lstUrl = _urls;
                lstKeyword = GetRandomItemsFromList(_keyWords, numElem);
            }
        }

        private void btnGenerateBackLink_Click(object sender, EventArgs e)
        {
            List<string> lstKeyword = null;
            List<string> lstUrl = null;

            GenerateBacklinks(ref lstKeyword, ref lstUrl);

            frmResult frm = new frmResult
            {
                Keywords = lstKeyword,
                Urls = lstUrl,
                TagType = rdbtnURL.Checked ? EnumTagType.TAG_URL : (rdbtnUrlNormal.Checked ? EnumTagType.TAG_URL_NORMAL : EnumTagType.TAG_A)
            };

            frm.ShowDialog();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtKeyword.Text = "";
            txtUrl.Text = "";
            txtUrl.Focus();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (txtRemove.Text == "")
            {
                MessageBox.Show("Vui long nhap url de xoa");
                return;
            }
            var errTxt = DataProvider.RemoveNode(txtUrl.Text.Trim());
            if (errTxt == "")
            {
                DataProvider.LoadDBInfo(ref _keyWords, ref _urls);
                MessageBox.Show("Da xoa");
            }
            else
            {
                MessageBox.Show(errTxt);
            }
        }
    }
}
